package com.chenjinyan.back.service.impl;

import com.chenjinyan.back.dao.LoginCountRepository;
import com.chenjinyan.back.entity.LoginCount;
import com.chenjinyan.back.service.LoginCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class LoginCountServiceImpl implements LoginCountService {

    @Autowired
    LoginCountRepository countRepository;


    @Override
    public Integer getLoginCountToday() {
        return countRepository.getLoginCountToday();
    }

    @Override
    public Integer getLoginCountMothy() {
        return countRepository.getLoginCountMonth();
    }

    @Override
    public Integer getActiveCountToday() {
        return countRepository.getActiveCountToday();
    }

    @Override
    public Integer getActiveCountMonth() {
        return countRepository.getActiveCountMonth();
    }

    @Override
    public Integer getActiveMessageCountToday() {
        return countRepository.getActiveMessageCountToday();
    }

    @Override
    public Integer getActiveMessageCountMonth() {
        return countRepository.getActiveMessageCountMonth();
    }

    @Override
    public List<LoginCount> getLoginCountByHour(String date) {
        List<Object[]> list = countRepository.getLoginCountByHour(date);
        List<LoginCount> saveList = new ArrayList<>();
        List<LoginCount> resultList = new ArrayList<>();
        String[] hours = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"};
        for (Object[] object : list) {
            LoginCount loginCount = new LoginCount();
            loginCount.setHour("hour");
            loginCount.setCount("count");
            Integer hour = (Integer) object[0];
            BigInteger count = (BigInteger) object[1];
            loginCount.setHourInt(hour);
            loginCount.setCountInt(count);
            saveList.add(loginCount);
        }
        if (list.size() > 0) {
            for (String hour : hours) {
                for (LoginCount loginCount : saveList) {
                    //boolean b = Arrays.asList(hours).contains(String.valueOf(loginCount.getHourInt()));
                    boolean b = false;
                    if (String.valueOf(loginCount.getHourInt()).equals(hour)) {
                        b = true;
                    }
                    if (b) {
                        resultList.add(loginCount);
                    } else {
                        resultList.add(setLoginCount(Integer.valueOf(hour)));
                    }
                }
            }
        } else {
            for (int i = 0; i < 24; i++) {
                resultList.add(setLoginCount(i));
            }
        }
        return resultList;
    }

    @Override
    public List<LoginCount> getLoginCountByDay(String date) {
        List<Object[]> list = countRepository.getLoginCountByDay(date);
        List<LoginCount> saveList = new ArrayList<>();
        List<LoginCount> resultList = new ArrayList<>();
        int dayCount = getCurrentMonthDay();
        String[] days = new String[dayCount];
        for (int i = 0; i < days.length; i++) {
            days[i] = String.valueOf(i + 1);
        }
        for (Object[] object : list) {
            LoginCount loginCount = new LoginCount();
            loginCount.setDay("day");
            loginCount.setCount("count");
            Integer day = (Integer) object[0];
            BigInteger count = (BigInteger) object[1];
            loginCount.setDayInt(day);
            loginCount.setCountInt(count);
            saveList.add(loginCount);
        }
        if (list.size() > 0) {
            for (String day : days) {
                for (LoginCount loginCount : saveList) {
                    //boolean b = Arrays.asList(hours).contains(String.valueOf(loginCount.getHourInt()));
                    boolean b = false;
                    if (String.valueOf(loginCount.getDayInt()).equals(day)) {
                        b = true;
                    }
                    if (b) {
                        resultList.add(loginCount);
                    } else {
                        resultList.add(setLoginCountDay(Integer.valueOf(day)));
                    }
                }
            }
        } else {
            for (int i = 0; i < 30; i++) {
                resultList.add(setLoginCount(i));
            }
        }
        return resultList;
    }

    @Override
    public List<LoginCount> getActiveCountByHour(String date) {
        List<Object[]> list = countRepository.getActiveByHour(date);
        List<LoginCount> saveList = new ArrayList<>();
        List<LoginCount> resultList = new ArrayList<>();
        String[] hours = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"};
        for (Object[] object : list) {
            LoginCount loginCount = new LoginCount();
            loginCount.setHour("hour");
            loginCount.setCount("count");
            Integer hour = (Integer) object[0];
            BigInteger count = (BigInteger) object[1];
            loginCount.setHourInt(hour);
            loginCount.setCountInt(count);
            saveList.add(loginCount);
        }
        if (list.size() > 0) {
            for (String hour : hours) {
                for (LoginCount loginCount : saveList) {
                    //boolean b = Arrays.asList(hours).contains(String.valueOf(loginCount.getHourInt()));
                    boolean b = false;
                    if (String.valueOf(loginCount.getHourInt()).equals(hour)) {
                        b = true;
                    }
                    if (b) {
                        resultList.add(loginCount);
                    } else {
                        resultList.add(setLoginCount(Integer.valueOf(hour)));
                    }
                }
            }
        } else {
            for (int i = 0; i < 24; i++) {
                resultList.add(setLoginCount(i));
            }
        }
        return resultList;
    }

    @Override
    public List<LoginCount> getActiveCountByDay(String date) {




        return null;
    }

    private LoginCount setLoginCount(int i) {
        LoginCount loginCount = new LoginCount();
        loginCount.setHour("hour");
        loginCount.setCount("count");
        Integer hour = i;
        BigInteger count = BigInteger.valueOf(0);
        loginCount.setHourInt(hour);
        loginCount.setCountInt(count);
        return loginCount;
    }

    private LoginCount setLoginCountDay(int i) {
        LoginCount loginCount = new LoginCount();
        loginCount.setHour("day");
        loginCount.setCount("count");
        Integer hour = i;
        BigInteger count = BigInteger.valueOf(0);
        loginCount.setHourInt(hour);
        loginCount.setCountInt(count);
        return loginCount;
    }


    /**
     * 获取当月的 天数
     *     
     */
    public static int getCurrentMonthDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }


}
